<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 猜字谜（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>小王设计了一个简单的猜字谜游戏&#xff0c;游戏的谜面是一个错误的单词&#xff0c;比如nesw&#xff0c;玩家需要猜出谜底库中正确的单词。猜中的要求如下&#xff1a;<br /> 对于某个谜面和谜底单词&#xff0c;满足下面任一条件都表示猜中&#xff1a;</p> 
<ol><li>变换顺序以后一样的&#xff0c;比如通过变换w和e的顺序&#xff0c;“nwes”跟“news”是可以完全对应的&#xff1b;</li><li>字母去重以后是一样的&#xff0c;比如“woood”和“wood”是一样的&#xff0c;它们去重后都是“wod”</li></ol> 
<p>请你写一个程序帮忙在谜底库中找到正确的谜底。谜面是多个单词&#xff0c;都需要找到对应的谜底&#xff0c;如果找不到的话&#xff0c;返回”not found”</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<ol><li>谜面单词列表&#xff0c;以“,”分隔</li><li>谜底库单词列表&#xff0c;以&#34;,&#34;分隔</li></ol> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<ul><li>匹配到的正确单词列表&#xff0c;以&#34;,&#34;分隔</li><li>如果找不到&#xff0c;返回&#34;not found&#34;</li></ul> 
<p></p> 
<h4>备注</h4> 
<ol><li>单词的数量N的范围&#xff1a;0 &lt; N &lt; 1000</li><li>词汇表的数量M的范围&#xff1a;0 &lt; M &lt; 1000</li><li>单词的长度P的范围&#xff1a;0 &lt; P &lt; 20</li><li>输入的字符只有小写英文字母&#xff0c;没有其他字符</li></ol> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">conection<br /> connection,today</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">connection</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:85px;">输入</td><td style="width:413px;">bdni,wooood<br /> bind,wrong,wood</td></tr><tr><td style="width:85px;">输出</td><td style="width:413px;">bind,wood</td></tr><tr><td style="width:85px;">说明</td><td style="width:413px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题有点歧义&#xff0c;那就是&#xff1a;</p> 
<blockquote> 
 <p>对于某个谜面和谜底单词&#xff0c;满足下面任一条件都表示猜中&#xff1a;</p> 
 <ol><li>变换顺序以后一样的&#xff0c;比如通过变换w和e的顺序&#xff0c;“nwes”跟“news”是可以完全对应的&#xff1b;</li><li>字母去重以后是一样的&#xff0c;比如“woood”和“wood”是一样的&#xff0c;它们去重后都是“wod”</li></ol> 
</blockquote> 
<p>那么如果两个条件都满足的话&#xff0c;算不算猜中呢&#xff1f;</p> 
<p>如果两个条件都满足也算猜中的话&#xff0c;那我们直接对谜底和谜面单词进行去重&#43;字典序排序&#xff0c;然后对比即可。</p> 
<p><s>如果两个条件都满足不算猜中&#xff0c;只有一个条件满足才算猜中的话&#xff0c;则需要对单词分别进行去重和字典序排序&#xff0c;然后对比两次。</s></p> 
<p><s>这里我给出两个情况的实现。</s></p> 
<p></p> 
<p>2023.02.21 根据机考网友反馈&#xff0c;本题使用“两个条件都满足”的解法可以获得100%通过率</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<p>两个条件都满足也算猜中</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const issues &#61; lines[0].split(&#34;,&#34;);
    const answers &#61; lines[1].split(&#34;,&#34;);
    console.log(getResult(issues, answers));
    lines.length &#61; 0;
  }
});

function getResult(issues, answers) {
  const ans &#61; [];

  for (let issue of issues) {
    const str1 &#61; [...new Set(issue)].sort().join(&#34;&#34;);
    let find &#61; false;

    for (let answer of answers) {
      const str2 &#61; [...new Set(answer)].sort().join(&#34;&#34;);
      if (str1 &#61;&#61;&#61; str2) {
        ans.push(answer);
        find &#61; true;
        // break; // 如果一个谜面对应多个谜底&#xff0c;这里就不能break&#xff0c;如果一个谜面只对应一个谜底&#xff0c;那这里就要break&#xff0c;考试的时候都试下
      }
    }

    if (!find) {
      ans.push(&#34;not found&#34;);
    }
  }

  return ans.join(&#34;,&#34;);
}
</code></pre> 
<p>唯一条件满足才算猜中</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const issues &#61; lines[0].split(&#34;,&#34;);
    const answers &#61; lines[1].split(&#34;,&#34;);
    console.log(getResult(issues, answers));
    lines.length &#61; 0;
  }
});

function getResult(issues, answers) {
  const ans &#61; [];

  for (let issue of issues) {
    const sorted_issue &#61; [...issue].sort().join(&#34;&#34;); // 排序后字符串
    const distinct_issue &#61; [...new Set(issue)].join(&#34;&#34;); // 去重后字符串
    let find &#61; false;

    for (let answer of answers) {
      const sorted_answer &#61; [...answer].sort().join(&#34;&#34;); // 排序后字符串
      const distinct_answer &#61; [...new Set(answer)].join(&#34;&#34;); // 去重后字符串

      if (
        sorted_issue &#61;&#61;&#61; sorted_answer ||
        distinct_issue &#61;&#61;&#61; distinct_answer
      ) {
        ans.push(answer);
        find &#61; true;
        // break; // 如果一个谜面对应多个谜底&#xff0c;这里就不能break&#xff0c;如果一个谜面只对应一个谜底&#xff0c;那这里就要break&#xff0c;考试的时候都试下
      }
    }

    if (!find) {
      ans.push(&#34;not found&#34;);
    }
  }

  return ans.join(&#34;,&#34;);
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<p>两个条件都满足也算猜中</p> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringJoiner;
import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] issues &#61; sc.nextLine().split(&#34;,&#34;);
    String[] answers &#61; sc.nextLine().split(&#34;,&#34;);

    System.out.println(getResult(issues, answers));
  }

  public static String getResult(String[] issues, String[] answers) {
    ArrayList&lt;String&gt; ans &#61; new ArrayList&lt;&gt;();

    for (String issue : issues) {
      String str1 &#61; getSortedAndDistinctStr(issue);
      boolean find &#61; false;

      for (String answer : answers) {
        String str2 &#61; getSortedAndDistinctStr(answer);
        if (str1.equals(str2)) {
          ans.add(answer);
          find &#61; true;
          // break; // 如果一个谜面对应多个谜底&#xff0c;这里就不能break&#xff0c;如果一个谜面只对应一个谜底&#xff0c;那这里就要break&#xff0c;考试的时候都试下
        }
      }

      if (!find) {
        ans.add(&#34;not found&#34;);
      }
    }

    StringJoiner sj &#61; new StringJoiner(&#34;,&#34;, &#34;&#34;, &#34;&#34;);
    for (String an : ans) {
      sj.add(an);
    }
    return sj.toString();
  }

  public static String getSortedAndDistinctStr(String str) {
    TreeSet&lt;Character&gt; set &#61; new TreeSet&lt;&gt;();
    for (char c : str.toCharArray()) set.add(c);
    return set.toString();
  }
}
</code></pre> 
<p>唯一条件满足才算猜中</p> 
<pre><code class="language-java">import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc &#61; new Scanner(System.in);

        String[] issues &#61; sc.nextLine().split(&#34;,&#34;);
        String[] answers &#61; sc.nextLine().split(&#34;,&#34;);

        System.out.println(getResult(issues, answers));
    }

    public static String getResult(String[] issues, String[] answers) {
        ArrayList&lt;String&gt; ans &#61; new ArrayList&lt;&gt;();

        for (String issue : issues) {
            String[] issueDeal &#61; getSortedAndDistinctStr(issue);
            boolean find &#61; false;

            for (String answer : answers) {
                String[] answerDeal &#61; getSortedAndDistinctStr(answer);

                if(issueDeal[0].equals(answerDeal[0]) || issueDeal[1].equals(answerDeal[1])) {
                    ans.add(answer);
                    find &#61; true;
                    // break; // 如果一个谜面对应多个谜底&#xff0c;这里就不能break&#xff0c;如果一个谜面只对应一个谜底&#xff0c;那这里就要break&#xff0c;考试的时候都试下
                }
            }

            if(!find) {
                ans.add(&#34;not found&#34;);
            }
        }

        StringJoiner sj &#61; new StringJoiner(&#34;,&#34;,&#34;&#34;,&#34;&#34;);
        for (String an : ans) {
            sj.add(an);
        }
        return sj.toString();
    }

    public static String[] getSortedAndDistinctStr(String str) {
        char[] arr &#61; str.toCharArray();
        Arrays.sort(arr);
        String sorted_str &#61; new String(arr); // 字典序排序后的字符串

        LinkedHashSet&lt;Character&gt; set &#61; new LinkedHashSet&lt;&gt;();
        for (char c : str.toCharArray()) set.add(c);
        String distinct_str &#61; set.toString(); // 去重后的字符串

        return new String[]{sorted_str, distinct_str};
    }
}</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<p>两个条件都满足也算猜中</p> 
<pre><code class="language-python"># 输入获取
issues &#61; input().split(&#34;,&#34;)
answers &#61; input().split(&#34;,&#34;)


# 算法入口
def getResult(issues, answers):
    ans &#61; []

    for issue in issues:
        str1 &#61; &#34;&#34;.join(sorted(set(issue)))
        find &#61; False

        for answer in answers:
            str2 &#61; &#34;&#34;.join(sorted(set(answer)))

            if str1 &#61;&#61; str2:
                ans.append(answer)
                find &#61; True
                # break # 如果一个谜面对应多个谜底&#xff0c;这里就不能break&#xff0c;如果一个谜面只对应一个谜底&#xff0c;那这里就要break&#xff0c;考试的时候都试下

        if not find:
            ans.append(&#34;not found&#34;)

    return &#34;,&#34;.join(ans)


# 算法调用
print(getResult(issues, answers))
</code></pre> 
<p></p> 
<p>唯一条件满足才算猜中</p> 
<pre><code class="language-python"># 输入获取
issues &#61; input().split(&#34;,&#34;)
answers &#61; input().split(&#34;,&#34;)


# 算法入口
def getResult(issues, answers):
    ans &#61; []

    for issue in issues:
        sorted_issue &#61; &#34;&#34;.join(sorted(issue))
        distinct_issue &#61; &#34;&#34;.join(set(issue))
        find &#61; False

        for answer in answers:
            sorted_answer &#61; &#34;&#34;.join(sorted(answer))
            distinct_answer &#61; &#34;&#34;.join(set(answer))

            if sorted_issue &#61;&#61; sorted_answer or distinct_issue &#61;&#61; distinct_answer:
                ans.append(answer)
                find &#61; True
                # break # 如果一个谜面对应多个谜底&#xff0c;这里就不能break&#xff0c;如果一个谜面只对应一个谜底&#xff0c;那这里就要break&#xff0c;考试的时候都试下

        if not find:
            ans.append(&#34;not found&#34;)

    return &#34;,&#34;.join(ans)


# 算法调用
print(getResult(issues, answers))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>